package com.masq.aboard.service.impl;

import com.masq.aboard.dao.ManagerUserRepository;
import com.masq.aboard.entity.ManagerUserEntity;
import com.masq.aboard.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @author masq
 * @since 2024-05-02
 */
@Service
@Transactional
public class UserDetailsServiceImpl implements UserDetailsService {

    private final ManagerUserRepository managerUserRepository;

    @Autowired
    public UserDetailsServiceImpl(ManagerUserRepository managerUserRepository) {
        this.managerUserRepository = managerUserRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ManagerUserEntity managerUserEntity = managerUserRepository.findManagerUserByUsername(username);
        if (managerUserEntity == null) {
            throw new UsernameNotFoundException("用户名或密码不正确");
        }
        return new UserModel(managerUserEntity);
    }
}
